<?php
namespace app\admin\controller;

use app\admin\common\Base;
use think\Db;
use think\Request;

class Carousel extends Base
{
	//首页大轮播图
	public function firstcar()
	{
		$data = Db::table('firstcar')->where(array('id'=>1))->find();
		$this->assign('data',$data);
		return $this->fetch();
	}
	public function car_save()
	{
		$id = 1;
		$data['imga'] = trim(input('post.imga'));
		$data['imgb'] = trim(input('post.imgb'));
		$data['imgc'] = trim(input('post.imgc'));
		$data['imgd'] = trim(input('post.imgd'));
		if($id){
			Db::table('firstcar')->where(array('id'=>1))->update($data);
		}
		exit(json_encode(array('code'=>0,'msg'=>'保存成功')));
	}
	//图片上传
	public function upload_img(){
		$file = request()->file('file');
		if($file==null){
			exit(json_encode(array('code'=>1,'msg'=>'没有文件上传')));
		}
		$info = $file->move(ROOT_PATH.'public'.DS.'uploads');
		$ext = ($info->getExtension());
		if(!in_array($ext,array('jpg','jpeg','gif','png'))){
			exit(json_encode(array('code'=>1,'msg'=>'文件格式不支持')));
		}
		$img = '/uploads/'.$info->getSaveName();
		exit(json_encode(array('code'=>0,'msg'=>$img)));
	}
	//近期特惠
	public function recent()
	{
		$data = Db::table('firstcar')->where(array('id'=>2))->find();
		$this->assign('data',$data);
		return $this->fetch();
	}
	public function recent_save()
	{
		$id = 2;
		$data['imga'] = trim(input('post.imga'));
		$data['imgb'] = trim(input('post.imgb'));
		$data['imgc'] = trim(input('post.imgc'));
		$data['imgd'] = trim(input('post.imgd'));
		$data['imge'] = trim(input('post.imge'));
		if($id){
			Db::table('firstcar')->where(array('id'=>2))->update($data);
		}
		exit(json_encode(array('code'=>0,'msg'=>'保存成功')));
	}
	//媒体报道
	public function media()
	{
		$data = Db::table('firstcar')->where(array('id'=>3))->find();
		$this->assign('data',$data);
		return $this->fetch();
	}
	public function media_save()
	{
		$id = 3;
		$data['imga'] = trim(input('post.imga'));
		$data['imgb'] = trim(input('post.imgb'));
		$data['imgc'] = trim(input('post.imgc'));
		$data['imgd'] = trim(input('post.imgd'));
		$data['imge'] = trim(input('post.imge'));
		if($id){
			Db::table('firstcar')->where(array('id'=>3))->update($data);
		}
		exit(json_encode(array('code'=>0,'msg'=>'保存成功')));
	}
	//蝶变日记
	public function butter()
	{
		$data = Db::table('firstcar')->where(array('id'=>4))->find();
		$this->assign('data',$data);
		return $this->fetch();
	}
	public function butter_save()
	{
		$id = 4;
		$data['imga'] = trim(input('post.imga'));
		$data['imgb'] = trim(input('post.imgb'));
		$data['imgc'] = trim(input('post.imgc'));
		if($id){
			Db::table('firstcar')->where(array('id'=>4))->update($data);
		}
		exit(json_encode(array('code'=>0,'msg'=>'保存成功')));
	}
}